package org.four.web.controller;

import org.four.common.model.ComUserBean;
import org.four.common.model.CompanyTree;
import org.four.common.model.LayuiPage;
import org.four.common.model.ZhaoPinBean;
import org.four.web.product.DelaySender;
import org.four.web.service.ZslService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("zsl")
@Controller
public class ZslController {

    @Autowired
    private ZslService zslService;

    /**
     * Es
     */
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private DelaySender delaySender;

    /**
     * 树
     * @return
     */
    @RequestMapping("findNavTree")
    @ResponseBody
    public List<CompanyTree> findNavTree(){
        return zslService.findNavTree();
    }

    /**
     * 新增招聘
     * @param zhaoPinBean
     * @return
     */
    @RequestMapping("addZhaoPin")
    @ResponseBody
    public Boolean addZhaoPin(ZhaoPinBean zhaoPinBean, HttpSession session){
        try {
            ComUserBean comUserBean = (ComUserBean) session.getAttribute(session.getId());
            zhaoPinBean.setComid(comUserBean.getId());
            ZhaoPinBean zhaoPinBeans = zslService.addZhaoPin(zhaoPinBean);
            if (zhaoPinBeans == null){
                return false;
            }
            //新增
            zhaoPinBean.setId(zhaoPinBeans.getId());
            zhaoPinBean.setFabudate(zhaoPinBeans.getFabudate());
            zhaoPinBean.setStatus(zhaoPinBeans.getStatus());
            ArrayList<IndexQuery> indexQueries = new ArrayList<>();
            IndexQuery indexQuery = new IndexQueryBuilder()
                    .withId(zhaoPinBeans.getId().toString())
                    .withObject(zhaoPinBean)
                    .build();
            indexQueries.add(indexQuery);
            elasticsearchTemplate.bulkIndex(indexQueries);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /*@RequestMapping("addZhaoPin")
    @ResponseBody
    public String addZhaoPin(ZhaoPinBean zhaoPinBean){
        if (zhaoPinBean.getId() == null){
            try {
                zslService.addZhaoPin(zhaoPinBean);
                return "1";
            }catch (Exception e){
                e.printStackTrace();
                return "0";
            }
        }
            return "0";
    }*/

    /**
     * 查询招聘信息
     * @param limit
     * @param page
     * @param zhaoPinBean
     * @return
     */
    @RequestMapping("queryZhaoPin")
    @ResponseBody
    public LayuiPage queryZhaoPin(int limit , int page,ZhaoPinBean zhaoPinBean,HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        zhaoPinBean.setComid(attribute.getId());
        return zslService.queryZhaoPin(limit,page,zhaoPinBean);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public String del(Integer[] id){
        try{
            zslService.delZhaoPin(id);
            return "1";
        }catch (Exception e){
            e.printStackTrace();
            return "0";
        }

    }

    /**
     * 查询投递信息
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping("queryTouDi")
    @ResponseBody
    public LayuiPage queryTouDi(int limit , int page,HttpSession session){
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        return zslService.queryTouDi(limit,page,attribute.getId());
    }

    /**
     * 修改投递信息状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("updatestatus")
    @ResponseBody
    public Boolean updatestatus(Integer id ,Integer status){
        try {
            zslService.updatestatus(id,status);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 支付前
     * @param id
     * @param temp
     * @return
     */
    @RequestMapping("adddingdan")
    @ResponseBody
    public Boolean adddingdan(Integer id,Integer temp,HttpSession session,Integer count){
        try {
            session.setAttribute("id",id);
            session.setAttribute("temp",temp);
            session.setAttribute("count",count);
            System.out.println("购买的id"+id+"前台传值"+temp);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改支付成功之后的
     * @param session
     * @return
     */
    @RequestMapping("dingdan1")
    public String dingdan1(HttpSession session){
        Integer id = (Integer) session.getAttribute("id");
        Integer temp = (Integer) session.getAttribute("temp");
        Integer count = (Integer) session.getAttribute("count");
        ComUserBean attribute = (ComUserBean) session.getAttribute(session.getId());
        if (temp== 1){
            zslService.dingdan(id);
            ZhaoPinBean zhaoPinBean = zslService.queryById(id);
            delaySender.sendDelay(zhaoPinBean);
        }
        if (temp== 2){
            zslService.addComUserCount(attribute.getId(),count);
        }

        return "company/main";
    }
    /**
     * 公司发布的岗位 数据
     * @return
     */
    @RequestMapping("findComZhaoPin")
    @ResponseBody
    public List findComZhaoPin(HttpSession session){
        System.out.println("公司发布的岗位 数据");
        ComUserBean comUserBean = (ComUserBean) session.getAttribute(session.getId());
        List list = zslService.findComZhaoPin(comUserBean.getId());
        return list;
    }

    @RequestMapping("findCount")
    @ResponseBody
    public Integer findCount(HttpSession session){
        ComUserBean comUserBean = (ComUserBean) session.getAttribute(session.getId());
        return zslService.findCount(comUserBean.getId());
    }
}
